import {
  queryGetApiGroupReq,
  queryGetDataApiDetailByIdReq,
  queryPublishApiByIdReq,
  queryRemoveDataApiByIdReq,
  queryRemoveDataApiGroupByIdReq,
  querySaveDataApiGroupReq,
  queryUnPublishApiByIdReq,
//  ---
  queryGetApiTagGroupReq,
  queryTheApiTagDetail,
  querySaveTagApiGroupReq,
  queryRemoveApiTagByIdReq,
  saveTheApiTagDetail,
} from './api'

export default {
  namespace: 'myApi',
  state: {
    dictionaryList: [],
    apiTagList: [],
  },
  effects: {
    // 左侧目录
    * queryGetApiGroup({ params, cb }, { call, put }) {
      const res = yield call(queryGetApiGroupReq, params)
      if (res && cb) {
        cb(res)
      }
      yield put({
        type: 'save',
        payload: {
          dictionaryList: res || [],
        },
      })
    },

    * queryPublishApiById({ params, cb }, { call, put }) {
      const res = yield call(queryPublishApiByIdReq, params)
      if (cb) {
        cb(res)
      }
    },

    * queryUnPublishApiById({ params, cb }, { call, put }) {
      const res = yield call(queryUnPublishApiByIdReq, params)
      if (cb) {
        cb(res)
      }
    },

    * queryGetDataApiDetailById({ params, cb }, { call, put }) {
      const res = yield call(queryGetDataApiDetailByIdReq, params)
      if (cb) {
        cb(res)
      }
    },

    * queryRemoveDataApiGroupById({ params, cb }, { call, put }) {
      const res = yield call(queryRemoveDataApiGroupByIdReq, params)
      if (cb) {
        cb(res)
      }
    },

    * querySaveDataApiGroup({ params, cb }, { call, put }) {
      const res = yield call(querySaveDataApiGroupReq, params)
      if (cb) {
        cb(res)
      }
    },

    * queryRemoveDataApiById({ params, cb }, { call, put }) {
      const res = yield call(queryRemoveDataApiByIdReq, params)
      if (cb) {
        cb(res)
      }
    },
    //---
    * queryGetApiTagGroup({ params, cb }, { call, put }) {
      const res = yield call(queryGetApiTagGroupReq, params)
      if (res && cb) {
        cb(res)
      }
      yield put({
        type: 'saveTag',
        payload: {
          apiTagList: res || [],
        },
      })
    },
    * querySaveApiTagGroup({ params, cb }, { call }) {
      const res = yield call(querySaveTagApiGroupReq, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryGetTheApiTagDetail({ params, cb }, { call }) {
      const res = yield call(queryTheApiTagDetail, params)
      if (res && cb) {
        cb(res)
      }
    },
    * queryRemoveTagApiGroupById({ params, cb }, { call }) {
      const res = yield call(queryRemoveApiTagByIdReq, params)
      if (cb) {
        cb(res)
      }
    },
    * saveTheApiTagDetail({ params, cb }, { call }) {
      const res = yield call(saveTheApiTagDetail, params)
      if (res && cb) {
        cb(res)
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
    saveTag(state, { payload }) {
      return {
        ...state,
        ...payload,
      }
    },
  },
}
